<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
			<el-form-item label="事件类型" prop="eType">
				<!--<el-input v-model="queryParams.eType" placeholder="请输入事件类型" clearable size="small" @keyup.enter.native="handleQuery" />-->
				<el-select style="width: 100%" v-model="queryParams.eType" clearable placeholder="请选择事件类型">
					<el-option v-for="item in exigencyEventList" :key="item.id" :label="item.eventName" :value="item.id.toString()"></el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="发生时间" prop="eCreateTime">
				<el-date-picker clearable size="small" style="width: 200px" v-model="queryParams.eCreateTime" type="date" value-format="yyyy-MM-dd" placeholder="选择发生时间"></el-date-picker>
			</el-form-item>
			<el-form-item label="结束时间" prop="eEndTime">
				<el-date-picker clearable size="small" style="width: 200px" v-model="queryParams.eEndTime" type="date" value-format="yyyy-MM-dd" placeholder="选择结束时间"></el-date-picker>
			</el-form-item>
			<el-form-item label="状态" prop="eStatus">
				<!--<el-input v-model="queryParams.eStatus" placeholder="请输入状态" clearable size="small" @keyup.enter.native="handleQuery" />-->
				<el-select style="width: 100%" v-model="queryParams.eStatus" clearable placeholder="请选择状态">
					<el-option v-for="dict in eventstatus" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue"></el-option>
				</el-select>
			</el-form-item>
			<!-- <el-form-item label="经度" prop="longitude">
        <el-input v-model="queryParams.longitude" placeholder="请输入经度" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="纬度" prop="latitude">
        <el-input v-model="queryParams.latitude" placeholder="请输入纬度" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item> -->
			<el-form-item label="道路方向" prop="direction">
				<!--<el-input v-model="queryParams.direction" placeholder="请输入道路方向" clearable size="small" @keyup.enter.native="handleQuery" />-->
				<el-select style="width: 100%" v-model="queryParams.direction" clearable placeholder="请选择道路方向">
					<el-option label="上行" value="上行"></el-option>
					<el-option label="下行" value="下行"></el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="来源" prop="source">
				<!--<el-input v-model="queryParams.source" placeholder="请输入来源" clearable size="small" @keyup.enter.native="handleQuery" />-->
				<el-select style="width: 100%" v-model="queryParams.source" clearable placeholder="请选择来源">
					<el-option v-for="dict in sourcelist" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue"></el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="事件详情" prop="details">
				<el-input v-model="queryParams.details" placeholder="请输入事件详情" clearable size="small" @keyup.enter.native="handleQuery" />
			</el-form-item>
			<!-- <el-form-item label="位置描述" prop="address">
        <el-input v-model="queryParams.address" placeholder="请输入位置描述" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="现场图片" prop="spare">
        <el-input v-model="queryParams.spare" placeholder="请输入现场图片" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item> -->
			<el-form-item>
				<el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
				<el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>

		<el-row :gutter="10" class="mb8">
			<el-col :span="1.5">
				<el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['manager:event:add']">新增</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="success" icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate" v-hasPermi="['manager:event:edit']">修改</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="danger" icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['manager:event:remove']">删除</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="warning" icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['manager:event:export']">导出</el-button>
			</el-col>
			<right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
		</el-row>

		<el-table v-loading="loading" :data="eventList" @selection-change="handleSelectionChange">
			<el-table-column type="selection" width="55" align="center" />
			<!-- <el-table-column label="ID" align="center" prop="eId" /> -->
			<el-table-column label="事件类型" align="center" prop="eType">
				<template slot-scope="scope">
					<span>{{ evevFilter(scope.row.eType) }}</span>
				</template>
			</el-table-column>
			<!-- <el-table-column label="状态" align="center" prop="eStatus" /> -->
			<!-- <el-table-column label="经度" align="center" prop="longitude" /> -->
			<!-- <el-table-column label="纬度" align="center" prop="latitude" /> -->
			<el-table-column label="发生时间" align="center" prop="eCreateTime" width="180">
				<template slot-scope="scope">
					<span>{{ scope.row.eCreateTime }}</span>
				</template>
			</el-table-column>
			<el-table-column label="道路方向" align="center" prop="direction" />
			<el-table-column label="来源" align="center" prop="source" :formatter="sourceFormat"/>
			<el-table-column label="事件详情" align="center" prop="details" />
			<el-table-column label="位置描述" align="center" prop="address" />
			<!--<el-table-column label="状态" align="center" prop="eStatus" :formatter="statusFormat" />-->
			<el-table-column label="状态" align="center" prop="eStatus" width="180">
				<template slot-scope="scope">
					<span v-if="scope.row.eStatus == 0" style="color: #f00;">未处理</span>
					<span v-if="scope.row.eStatus == 1" style="color: #f7d904;">处理中</span>
					<span v-if="scope.row.eStatus == 2" style="color: #0294fd;">已结束</span>
				</template>
			</el-table-column>
			<el-table-column label="结束时间" align="center" prop="eEndTime" width="180">
				<template slot-scope="scope">
					<span>{{ scope.row.eEndTime }}</span>
				</template>
			</el-table-column>
			<!--<el-table-column label="现场图片" align="center" prop="spare" />-->
			<el-table-column label="操作" align="center" class-name="small-padding fixed-width">
				<template slot-scope="scope">
					<el-button size="mini" type="text" icon="el-icon-document-copy" @click="handleUpdate(scope.row)">查看</el-button>
					<el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['manager:event:remove']">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

		<!-- 添加或修改应急事件信息对话框 -->
		<el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
			<el-form ref="form" :model="form" :rules="rules" label-width="80px">
				<el-form-item label="事件类型" prop="eType">
					<el-select style="width: 100%" v-model="form.eType" disabled placeholder="请输入事件类型">
						<el-option v-for="item in exigencyEventList" :key="item.id" :label="item.eventName" :value="item.id.toString()"></el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="道路方向" prop="direction" style="display: inline-block;width: 49%;">
					<el-input v-model="form.direction" disabled placeholder="请输入道路方向" />
				</el-form-item>
				<el-form-item label="来源" prop="source" style="display: inline-block;width: 49%;">
					<el-input v-model="form.source" disabled placeholder="请输入来源" />
				</el-form-item>
				<el-form-item label="事件详情" prop="details">
					<el-input v-model="form.details" disabled placeholder="请输入事件详情" />
				</el-form-item>
				<el-form-item label="位置描述" prop="details">
					<el-input v-model="form.address" disabled placeholder="请输入位置描述" />
				</el-form-item>
				<el-form-item label="现场图片" prop="spare">
					<el-image v-for="item in form.spareList" style="width: 100px; height: 100px" :src="item" :preview-src-list="form.spareList" fit="contain"></el-image>
					<!--<el-input v-model="form.spare" placeholder="请输入现场图片" />-->
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<!--<el-button type="primary" @click="submitForm">确 定</el-button>-->
				<el-button @click="cancel">取 消</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
	import { listEvent, getEvent, delEvent, addEvent, updateEvent, exportEvent } from '@/api/event/eventInfo';

	// 事件列表
	import { listExigencyEvent } from '@/api/event/eventType';

	export default {
		name: 'Event',
		data() {
			return {
				// 遮罩层
				loading: true,
				// 选中数组
				ids: [],
				// 非单个禁用
				single: true,
				// 非多个禁用
				multiple: true,
				// 显示搜索条件
				showSearch: true,
				// 总条数
				total: 0,
				// 应急事件信息表格数据
				eventList: [],
				// 弹出层标题
				title: '',
				// 是否显示弹出层
				open: false,
				// 查询参数
				queryParams: {
					pageNum: 1,
					pageSize: 10,
					eType: null,
					eCreateTime: null,
					eEndTime: null,
					eStatus: null,
					longitude: null,
					latitude: null,
					direction: null,
					source: null,
					details: null,
					spare: null,
					switchFlag: 1
				},
				// 表单参数
				form: {},
				// 表单校验
				rules: {},
				eventstatus: [],
				sourcelist: [],
				exigencyEventList: [] // 事件列表
			};
		},
		created() {
			this.getList();
			this.getDicts('event_traceability_status').then(response => {
				this.eventstatus = response.data;
			});
			this.getDicts('source_incident').then(response => {
				this.sourcelist = response.data;
			});
		},
		methods: {
			/** 查询应急事件信息列表 */
			getList() {
				this.loading = true;
				listEvent(this.queryParams).then(response => {
					this.eventList = response.rows;
					this.total = response.total;
					this.loading = false;
				});

				listExigencyEvent().then(response => {
					this.exigencyEventList = response.rows;
					console.log(response.rows);
				});
			},
			// 字典翻译
			statusFormat(row, column) {
				return this.selectDictLabel(this.eventstatus, row.eStatus);
			},
			sourceFormat(row, column) {
				return this.selectDictLabel(this.sourcelist, row.source);
			},
			// 取消按钮
			cancel() {
				this.open = false;
				this.reset();
			},
			// 表单重置
			reset() {
				this.form = {
					eId: null,
					eType: null,
					eCreateTime: null,
					eEndTime: null,
					eStatus: null,
					longitude: null,
					latitude: null,
					direction: null,
					source: null,
					details: null,
					spare: null
				};
				this.resetForm('form');
			},
			/** 搜索按钮操作 */
			handleQuery() {
				this.queryParams.pageNum = 1;
				this.getList();
			},
			/** 重置按钮操作 */
			resetQuery() {
				this.resetForm('queryForm');
				this.handleQuery();
			},
			// 多选框选中数据
			handleSelectionChange(selection) {
				this.ids = selection.map(item => item.eId);
				this.single = selection.length !== 1;
				this.multiple = !selection.length;
			},
			/** 新增按钮操作 */
			handleAdd() {
				this.reset();
				this.open = true;
				this.title = '添加应急事件信息';
			},
			/** 修改按钮操作 */
			handleUpdate(row) {
				this.reset();
				const eId = row.eId || this.ids;
				getEvent(eId).then(response => {
					for(var i=0;i<response.data.spareList.length;i++){
						response.data.spareList[i] = process.env.VUE_APP_BASE_API + response.data.spareList[i]
					}
					this.form = response.data;
//					console.log(this.form,process.env.VUE_APP_BASE_API)
					this.open = true;
					this.title = '应急事件信息';
				});
			},
			/** 提交按钮 */
			submitForm() {
				this.$refs['form'].validate(valid => {
					if(valid) {
						if(this.form.eId != null) {
							updateEvent(this.form).then(response => {
								this.msgSuccess('修改成功');
								this.open = false;
								this.getList();
							});
						} else {
							addEvent(this.form).then(response => {
								this.msgSuccess('新增成功');
								this.open = false;
								this.getList();
							});
						}
					}
				});
			},
			/** 删除按钮操作 */
			handleDelete(row) {
				const eIds = row.eId || this.ids;
				this.$confirm('是否确认删除应急事件信息编号为"' + eIds + '"的数据项?', '警告', {
						confirmButtonText: '确定',
						cancelButtonText: '取消',
						type: 'warning'
					})
					.then(function() {
						return delEvent(eIds);
					})
					.then(() => {
						this.getList();
						this.msgSuccess('删除成功');
					});
			},
			/** 导出按钮操作 */
			handleExport() {
				const queryParams = this.queryParams;
				this.$confirm('是否确认导出所有应急事件信息数据项?', '警告', {
						confirmButtonText: '确定',
						cancelButtonText: '取消',
						type: 'warning'
					})
					.then(function() {
						return exportEvent(queryParams);
					})
					.then(response => {
						this.download(response.msg);
					});
			},
			// 过滤信息
			evevFilter(id) {
				for(let [key, val] of Object.entries(this.exigencyEventList)) {
					if(val.id == id) {
						return val.eventName;
					}
				}
			}
		}
	};
</script>